<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');


// Modified the original downloader to print the file to output instead
//  of reading it into a variable, to prevent running up our memory limit

function force_download($path, $filename, $alt_name = '')
{
    if (!is_file($path . '/' . $filename)) {
        show_404();
    }

    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type
    if (FALSE === strpos($filename, '.')) {
        show_404();
    }

    // Grab the file extension
    $x = explode('.', $filename);
    $extension = end($x);

    // Load the mime types
    @include(APPPATH . 'config/mimes' . EXT);

    // Set a default mime if we can't find it
    if (!isset($mimes[$extension])) {
        $mime = 'application/octet-stream';
    } else {
        $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
    }

    // send the file as a different name to the user
    if ($alt_name) $final_name = $alt_name;
    else $final_name = $filename;

    // Generate the server headers
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $final_name . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: " . filesize($path . $filename));
    } else {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $final_name . '"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: " . filesize($path . '/' . $filename));
    }

    $fp = fopen($path . '/' . $filename, "r");
    print fread($fp, filesize($path . '/' . $filename));
    fclose($fp);
    exit();

}


// Send any content to the user as a file

function force_download_content($filename, $content)
{

    // Grab the file extension
    $x = explode('.', $filename);
    $extension = end($x);

    // Load the mime types
    @include(APPPATH . 'config/mimes' . EXT);

    // Set a default mime if we can't find it
    if (!isset($mimes[$extension])) {
        $mime = 'application/octet-stream';
    } else {
        $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
    }

    // Generate the server headers
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        //header("Content-Length: ".filesize($path.$filename));
    } else {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        //header("Content-Length: ".filesize($path.'/'.$filename));
    }

    echo $content;

}

